+Tue Jan 20 13:07:04 2004 Jonathan Blandford <jrb@gnome.org>
+
+ * xdgmime.c: resync with upstream sources.
+
Fri Oct 24 16:54:57 2003 Owen Taylor <otaylor@redhat.com>
* Makefile.am (libxdgmime_la_SOURCES): Add .h files to
#include <sys/types.h>
#include <unistd.h>
-XdgGlobHash *global_hash = NULL;
-XdgMimeMagic *global_magic = NULL;
-
+static int initted = 0;
+static XdgGlobHash *global_hash = NULL;
+static XdgMimeMagic *global_magic = NULL;
+const char *xdg_mime_type_unknown = "application/octet-stream";
static void
_xdg_mime_init_from_directory (const char *directory)
static void
xdg_mime_init (void)
{
- static int initted = 0;
-
if (initted == 0)
{
const char *xdg_data_home;
len = end_ptr - ptr;
else
len = end_ptr - ptr + 1;
- dir = malloc (len);
+ dir = malloc (len + 1);
strncpy (dir, ptr, len);
+ dir[len] = '\0';
_xdg_mime_init_from_directory (dir);
free (dir);
*/
return _xdg_utf8_validate (mime_type);
}
+
+void
+xdg_mime_shutdown (void)
+{
+ /* FIXME: Need to make this (and the whole library) thread safe */
+ if (initted)
+ {
+ _xdg_glob_hash_free (global_hash);
+ global_hash = NULL;
+
+ _xdg_mime_magic_free (global_magic);
+ global_magic = NULL;
+
+ initted = 0;
+ }
+}
extern "C" {
#endif /* __cplusplus */
-#define XDG_MIME_TYPE_UNKNOWN "application/octet-stream"
+extern const char *xdg_mime_type_unknown;
+#define XDG_MIME_TYPE_UNKNOWN xdg_mime_type_unknown
#ifdef XDG_PREFIX
#define XDG_ENTRY(func) _XDG_ENTRY2(XDG_PREFIX,func)
#define _XDG_ENTRY2(prefix,func) _XDG_ENTRY3(prefix,func)
#define _XDG_ENTRY3(prefix,func) prefix##_##func
+#endif
+
+#ifdef XDG_PREFIX
#define xdg_mime_get_mime_type_for_data XDG_ENTRY(get_mime_type_for_data)
#define xdg_mime_get_mime_type_for_file XDG_ENTRY(get_mime_type_for_file)
#define xdg_mime_get_mime_type_from_file_name XDG_ENTRY(get_mime_type_from_file_name)
#define xdg_mime_is_valid_mime_type XDG_ENTRY(is_valid_mime_type)
+#define xdg_mime_type_unknown XDG_ENTRY(type_unknown)
#endif
const char *xdg_mime_get_mime_type_for_data (const void *data,
const char *xdg_mime_get_mime_type_for_file (const char *file_name);
const char *xdg_mime_get_mime_type_from_file_name (const char *file_name);
int xdg_mime_is_valid_mime_type (const char *mime_type);
+void xdg_mime_shutdown (void);
#ifdef __cplusplus
}
/* -*- mode: C; c-file-style: "gnu" -*- */
-/* xdgmime.c: Private file. Datastructure for storing the globs.
+/* xdgmimeglob.c: Private file. Datastructure for storing the globs.
*
* More info can be found at http://www.freedesktop.org/standards/
*
*
* Licensed under the Academic Free License version 2.0
* Or under the following terms:
- *
+ *
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
return new_element;
}
-#if 0
+/* Frees glob_list and all of it's children */
static void
_xdg_glob_list_free (XdgGlobList *glob_list)
{
- free (glob_list);
+ XdgGlobList *ptr, *next;
+
+ ptr = glob_list;
+
+ while (ptr != NULL)
+ {
+ next = ptr->next;
+
+ if (ptr->data)
+ free ((void *) ptr->data);
+ if (ptr->mime_type)
+ free ((void *) ptr->mime_type);
+ free (ptr);
+
+ ptr = next;
+ }
}
-#endif
static XdgGlobList *
_xdg_glob_list_append (XdgGlobList *glob_list,
return glob_hash_node;
}
-#if 0
-static void
-_xdg_glob_hash_node_free (XdgGlobHashNode *glob_hash_node)
-{
- free (glob_hash_node);
-}
-#endif
-
void
_xdg_glob_hash_node_dump (XdgGlobHashNode *glob_hash_node,
int depth)
void
_xdg_glob_hash_free (XdgGlobHash *glob_hash)
{
+ _xdg_glob_list_free (glob_hash->literal_list);
+ _xdg_glob_list_free (glob_hash->full_list);
_xdg_glob_hash_free_nodes (glob_hash->simple_node);
free (glob_hash);
}
-
-
XdgGlobType
_xdg_glob_determine_type (const char *glob)
{
/* -*- mode: C; c-file-style: "gnu" -*- */
-/* xdgmime.h: Private file. Datastructure for storing the globs.
+/* xdgmimeglob.h: Private file. Datastructure for storing the globs.
*
* More info can be found at http://www.freedesktop.org/standards/
- *
+ *
* Copyright (C) 2003 Red Hat, Inc.
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
*
* Licensed under the Academic Free License version 2.0
* Or under the following terms:
- *
+ *
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
#ifndef __XDG_MIME_GLOB_H__
#define __XDG_MIME_GLOB_H__
+#include "xdgmime.h"
typedef struct XdgGlobHash XdgGlobHash;
XDG_GLOB_FULL, /* x*.[ch] */
} XdgGlobType;
+
+#ifdef XDG_PREFIX
+#define _xdg_mime_glob_read_from_file XDG_ENTRY(glob_read_from_file)
+#define _xdg_glob_hash_new XDG_ENTRY(hash_new)
+#define _xdg_glob_hash_free XDG_ENTRY(hash_free)
+#define _xdg_glob_hash_lookup_file_name XDG_ENTRY(hash_lookup_file_name)
+#define _xdg_glob_hash_append_glob XDG_ENTRY(hash_append_glob)
+#define _xdg_glob_determine_type XDG_ENTRY(determine_type)
+#define _xdg_glob_hash_dump XDG_ENTRY(hash_dump)
+#endif
void _xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash,
const char *file_name);
/* xdgmimeint.c: Internal defines and functions.
*
* More info can be found at http://www.freedesktop.org/standards/
- *
+ *
* Copyright (C) 2003 Red Hat, Inc.
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
*
* Licensed under the Academic Free License version 2.0
* Or under the following terms:
- *
+ *
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
#define TRUE (!FALSE)
#endif
-const unsigned char utf8_skip_data[256] = {
+const unsigned char _xdg_utf8_skip_data[256] = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
};
-const char * const utf8_skip = utf8_skip_data;
+const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
{
int bytelength = 0;
xdg_unichar_t result;
- if ( ! *source & 0x40 )
+ if ( ! (*source & 0x40) )
{
ucs32 = *source;
}
else
{
- if ( ! *source & 0x20 )
+ if ( ! (*source & 0x20) )
{
result = *source++ & 0x1F;
bytelength = 2;
}
- else if ( ! *source & 0x10 )
+ else if ( ! (*source & 0x10) )
{
result = *source++ & 0x0F;
bytelength = 3;
}
- else if ( ! *source & 0x08 )
+ else if ( ! (*source & 0x08) )
{
result = *source++ & 0x07;
bytelength = 4;
}
- else if ( ! *source & 0x04 )
+ else if ( ! (*source & 0x04) )
{
result = *source++ & 0x03;
bytelength = 5;
}
- else if ( ! *source & 0x02 )
+ else if ( ! (*source & 0x02) )
{
result = *source++ & 0x01;
bytelength = 6;
/* xdgmimeint.h: Internal defines and functions.
*
* More info can be found at http://www.freedesktop.org/standards/
- *
+ *
* Copyright (C) 2003 Red Hat, Inc.
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
*
* Licensed under the Academic Free License version 2.0
* Or under the following terms:
- *
+ *
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
#ifndef __XDG_MIME_INT_H__
#define __XDG_MIME_INT_H__
+#include "xdgmime.h"
+
/* FIXME: Should be configure check */
typedef unsigned int xdg_unichar_t;
typedef unsigned char xdg_uchar8_t;
typedef unsigned short xdg_uint16_t;
typedef unsigned int xdg_uint32_t;
+#ifdef XDG_PREFIX
+#define _xdg_utf8_skip XDG_ENTRY(utf8_skip)
+#define _xdg_utf8_to_ucs4 XDG_ENTRY(utf8_to_ucs4)
+#define _xdg_ucs4_to_upper XDG_ENTRY(ucs4_to_upper)
+#define _xdg_utf8_validate XDG_ENTRY(utf8_validate)
+#define _xdg_get_base_name XDG_ENTRY(get_ase_name)
+#endif
#define SWAP_BE16_TO_LE16(val) (xdg_uint16_t)(((xdg_uint16_t)(val) << 8)|((xdg_uint16_t)(val) >> 8))
(((xdg_uint32_t)(val) & 0x000000FFU) << 24))
/* UTF-8 utils
*/
-const char *const utf8_skip;
-#define _xdg_utf8_next_char(p) (char *)((p) + utf8_skip[*(unsigned char *)(p)])
-#define _xdg_utf8_char_size(p) (int) (utf8_skip[*(unsigned char *)(p)])
+const char *const _xdg_utf8_skip;
+#define _xdg_utf8_next_char(p) (char *)((p) + _xdg_utf8_skip[*(unsigned char *)(p)])
+#define _xdg_utf8_char_size(p) (int) (_xdg_utf8_skip[*(unsigned char *)(p)])
xdg_unichar_t _xdg_utf8_to_ucs4 (const char *source);
xdg_unichar_t _xdg_ucs4_to_upper (xdg_unichar_t source);
-
/* -*- mode: C; c-file-style: "gnu" -*- */
-/* xdgmimemagic.: Private file. Datastructure for storing magic.
+/* xdgmimemagic.: Private file. Datastructure for storing magic files.
*
* More info can be found at http://www.freedesktop.org/standards/
- *
+ *
* Copyright (C) 2003 Red Hat, Inc.
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
*
* Licensed under the Academic Free License version 2.0
* Or under the following terms:
- *
+ *
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* Boston, MA 02111-1307, USA.
*/
+#include <assert.h>
#include "xdgmimemagic.h"
#include "xdgmimeint.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
return matchlet;
}
-void
-_xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
-{
- if (mime_magic_match)
- {
- if (mime_magic_match->mime_type)
- free ((char *)mime_magic_match->mime_type);
- free (mime_magic_match);
- }
-}
-
void
_xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
}
+/* Frees mime_magic_match and the remainder of its list
+ */
+void
+_xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
+{
+ XdgMimeMagicMatch *ptr, *next;
+
+ ptr = mime_magic_match;
+ while (ptr)
+ {
+ next = ptr->next;
+
+ if (ptr->mime_type)
+ free ((void *) ptr->mime_type);
+ if (ptr->matchlet)
+ _xdg_mime_magic_matchlet_free (ptr->matchlet);
+ free (ptr);
+
+ ptr = next;
+ }
+}
/* Reads in a hunk of data until a newline character or a '\000' is hit. The
* returned string is null terminated, and doesn't include the newline.
retval = realloc (retval, len);
}
}
-
+
retval[pos] = '\000';
return retval;
}
char number_string[MAX_NUMBER_SIZE];
int pos = 0;
int c;
- int retval = -1;
+ long retval = -1;
while (TRUE)
{
if (pos > 0)
{
number_string[pos] = '\000';
+ errno = 0;
retval = strtol (number_string, NULL, 10);
- if ((retval == LONG_MIN || retval == LONG_MAX) &&
- (errno == ERANGE))
+
+ if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
return -1;
}
char *end_ptr;
int end_of_file = 0;
- assert (magic_file);
- assert (match);
+ assert (magic_file != NULL);
+ assert (match != NULL);
c = fgetc (magic_file);
if (c == EOF)
int indent = 0;
int bytes_read;
- assert (magic_file);
+ assert (magic_file != NULL);
/* Sniff the buffer to make sure it's a valid line */
c = fgetc (magic_file);
if (c != '>')
return XDG_MIME_MAGIC_ERROR;
-
+
matchlet = _xdg_mime_magic_matchlet_new ();
matchlet->indent = indent;
matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
matchlet->next = match->matchlet;
match->matchlet = matchlet;
-
+
return XDG_MIME_MAGIC_MAGIC;
}
size_t len)
{
int i, j;
-
+
for (i = matchlet->offset; i <= matchlet->offset + matchlet->range_length; i++)
{
int valid_matchlet = TRUE;
return;
}
- if (match->priority < mime_magic->match_list->priority)
+ if (match->priority > mime_magic->match_list->priority)
{
match->next = mime_magic->match_list;
mime_magic->match_list = match;
size_t len)
{
XdgMimeMagicMatch *match;
-
+
for (match = mime_magic->match_list; match; match = match->next)
{
if (_xdg_mime_magic_match_compare_to_data (match, data, len))
return match->mime_type;
}
}
-
+
return NULL;
}
{
XdgMimeMagicMatch *match;
int max_extent = 0;
-
+
for (match = mime_magic->match_list; match; match = match->next)
{
XdgMimeMagicMatchlet *matchlet;
/* -*- mode: C; c-file-style: "gnu" -*- */
-/* xdgmimemagic.h: Private file. Datastructure for storing the globs.
+/* xdgmimemagic.h: Private file. Datastructure for storing the magic files.
*
* More info can be found at http://www.freedesktop.org/standards/
- *
+ *
* Copyright (C) 2003 Red Hat, Inc.
* Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
*
* Licensed under the Academic Free License version 2.0
* Or under the following terms:
- *
+ *
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
#define __XDG_MIME_MAGIC_H__
#include <unistd.h>
-
+#include "xdgmime.h"
typedef struct XdgMimeMagic XdgMimeMagic;
+#ifdef XDG_PREFIX
+#define _xdg_mime_glob_read_from_file XDG_ENTRY(glob_read_from_file)
+#define _xdg_mime_magic_new XDG_ENTRY(magic_new)
+#define _xdg_mime_magic_read_from_file XDG_ENTRY(magic_read_from_file)
+#define _xdg_mime_magic_free XDG_ENTRY(magic_free)
+#define _xdg_mime_magic_get_buffer_extents XDG_ENTRY(magic_get_buffer_extents)
+#define _xdg_mime_magic_lookup_data XDG_ENTRY(magic_lookup_data)
+#endif
+
+
XdgMimeMagic *_xdg_mime_magic_new (void);
void _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
const char *file_name);